﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;
using YJKCodeBase.XmlUtility;
using ModelChecking.ModelValidation.Commands;

namespace ModelChecking.ModelValidation.ClearHeightCheck
{
	public partial class ShowClearHeightForm : System.Windows.Forms.Form
	{
		public ShowClearHeightForm(Dictionary<Level, List<ClearHeightArea>> LevelItemInfo, ExternalEvent exEvent, RequestHandler handler)
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.m_Handler = handler;
			this.m_ExEvent = exEvent;
			this.m_LevelItemInfo = LevelItemInfo;
			this.initTreeView();
			this.xmlHelper.LoadXml(this.path, "Root");
			string text = "2.1";
			this.textBoxHeight.Text = text;
		}

		public void initTreeView()
		{
			this.treeViewLevels.Nodes.Clear();
			this.treeViewLevels.LabelEdit = false;
			foreach (Level level in this.m_LevelItemInfo.Keys)
			{
				int num = this.CalculateElementCount(this.m_LevelItemInfo[level]);
				TreeNode treeNode = new TreeNode();
				treeNode.Text = level.Name + "（" + num.ToString() + "个）";
				this.initAreaNode(treeNode, this.m_LevelItemInfo[level]);
				this.treeViewLevels.Nodes.Add(treeNode);
			}
		}

		private int CalculateElementCount(List<ClearHeightArea> listClearArea)
		{
			int num = 0;
			foreach (ClearHeightArea clearHeightArea in listClearArea)
			{
				List<ElementAndClearHeight> elementItems = clearHeightArea.getElementItems();
				foreach (ElementAndClearHeight elementAndClearHeight in elementItems)
				{
					num += elementAndClearHeight.getElementList().Count;
				}
			}
			return num;
		}

		private void initAreaNode(TreeNode rootNode, List<ClearHeightArea> listClearHeight)
		{
			int num = 1;
			foreach (ClearHeightArea clearHeightArea in listClearHeight)
			{
				TreeNode treeNode = new TreeNode();
				treeNode.Text = "区域" + num.ToString();
				num++;
				this.initElementNode(treeNode, clearHeightArea.getElementItems());
				rootNode.Nodes.Add(treeNode);
			}
		}

		private void initElementNode(TreeNode rootNode, List<ElementAndClearHeight> elementClearHeight)
		{
			if (elementClearHeight.Count < 1)
			{
				TreeNode treeNode = new TreeNode();
				treeNode.Text = "无";
				rootNode.Nodes.Add(treeNode);
			}
			foreach (ElementAndClearHeight elementAndClearHeight in elementClearHeight)
			{
				double clearHeight = elementAndClearHeight.getClearHeight();
				foreach (Element element in elementAndClearHeight.getElementList())
				{
					TreeNode treeNode2 = new TreeNode();
					int integerValue = element.Id.IntegerValue;
					string elementName = this.getElementName(element);
					string clearHeightString = this.getClearHeightString(clearHeight);
					treeNode2.Text = string.Concat(new string[]
					{
						integerValue.ToString(),
						" ",
						elementName,
						" ",
						clearHeightString,
						"m"
					});
					treeNode2.Tag = element;
					rootNode.Nodes.Add(treeNode2);
				}
			}
		}

		private string getClearHeightString(double value)
		{
			int num = (int)(value + 0.5);
			return ((double)num / 1000.0).ToString();
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			this.m_Handler.Request.Make(RequestId.Remove);
			this.m_ExEvent.Raise();
			this.SaveDlgData();
			base.Close();
		}

		private void btnReflush_Click(object sender, EventArgs e)
		{
			string text = this.textBoxHeight.Text;
			double spaceHeight = Convert.ToDouble(text);
			this.m_LevelItemInfo.Clear();
			ApplicationClearHeight.thisApp.m_fun.setSpaceHeight(spaceHeight);
			this.m_Handler.Request.Make(RequestId.reCalculation);
			this.m_ExEvent.Raise();
		}

		private void btnSel_Click(object sender, EventArgs e)
		{
			Element element = null;
			if (this.treeViewLevels.SelectedNode != null && this.treeViewLevels.SelectedNode.Tag != null)
			{
				element = (Element)this.treeViewLevels.SelectedNode.Tag;
			}
			if (element != null)
			{
				int wMsg = 7;
				ShowClearHeightForm.SendMessage(ClearHeightCheckCmd._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				ApplicationClearHeight.thisApp.m_fun.setZoomElement(element);
				this.m_Handler.Request.Make(RequestId.ZoomElement);
				this.m_ExEvent.Raise();
			}
		}

		private void treeViewLevels_AfterSelect(object sender, TreeViewEventArgs e)
		{
		}

		private string getElementName(Element element)
		{
			string result;
			if (element is Pipe || element is Duct)
			{
				string str = element.GetParameter(BuiltInParameter.ELEM_TYPE_PARAM).AsValueString();
				string str2 = element.GetParameter(BuiltInParameter.RBS_SYSTEM_CLASSIFICATION_PARAM).AsString();
				result = str2 + "-" + str;
			}
			else
			{
				result = element.GetParameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM).AsValueString();
			}
			return result;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void treeViewLevels_DoubleClick(object sender, EventArgs e)
		{
			Element element = null;
			if (this.treeViewLevels.SelectedNode != null && this.treeViewLevels.SelectedNode.Tag != null)
			{
				element = (Element)this.treeViewLevels.SelectedNode.Tag;
			}
			if (element != null)
			{
				int wMsg = 7;
				ShowClearHeightForm.SendMessage(ClearHeightCheckCmd._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				ApplicationClearHeight.thisApp.m_fun.setZoomElement(element);
				this.m_Handler.Request.Make(RequestId.ZoomElement);
				this.m_ExEvent.Raise();
			}
		}

		private void ShowClearHeightForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.m_Handler.Request.Make(RequestId.Remove);
			this.m_ExEvent.Raise();
		}

		private void btnPickElement_Click(object sender, EventArgs e)
		{
			int wMsg = 7;
			ShowClearHeightForm.SendMessage(ClearHeightCheckCmd._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			this.m_Handler.Request.Make(RequestId.PickElements);
			this.m_ExEvent.Raise();
		}

		public void RefreshTreeView(List<ElementId> listElement, List<Element> elementList)
		{
			foreach (object obj in this.treeViewLevels.Nodes)
			{
				TreeNode node = (TreeNode)obj;
				this.DeleteBackColor(node);
			}
			foreach (object obj2 in this.treeViewLevels.Nodes)
			{
				TreeNode node2 = (TreeNode)obj2;
				this.ChangeNodeBackColor(node2, listElement, elementList);
			}
		}

		private void DeleteBackColor(TreeNode node)
		{
			if (node.Nodes.Count > 0)
			{
				IEnumerator enumerator = node.Nodes.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						TreeNode node2 = (TreeNode)obj;
						this.DeleteBackColor(node2);
					}
					return;
				}
			}
            if (node.BackColor == System.Drawing.Color.Red)
			{
                node.BackColor = System.Drawing.Color.Transparent;
			}
		}

		private bool ChangeNodeBackColor(TreeNode node, List<ElementId> listElement, List<Element> elementList)
		{
			if (node.Nodes.Count > 0)
			{
				IEnumerator enumerator = node.Nodes.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						TreeNode node2 = (TreeNode)obj;
						if (this.ChangeNodeBackColor(node2, listElement, elementList))
						{
							node.Expand();
						}
					}
					return false;
				}
			}
			if (node.Tag != null)
			{
				Element element = node.Tag as Element;
				for (int i = 0; i < listElement.Count; i++)
				{
					if (element.Id.Equals(listElement[i]))
					{
                        node.BackColor = System.Drawing.Color.Red;
						listElement.RemoveAt(i);
						elementList.Add(element);
						return true;
					}
				}
			}
			return false;
		}

		private void ShowClearHeightForm_Load(object sender, EventArgs e)
		{
			this.LoadDlgData();
		}

		private void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "CheckTypeSelectForm"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "CheckTypeSelectForm", "Params"));
			this.xmlHelper.SetAttribute("X", base.Location.X.ToString());
			this.xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			this.xmlHelper.SaveXml("");
		}

		private void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "CheckTypeSelectForm", "Params"));
			string attribute = this.xmlHelper.GetAttribute("X");
			string attribute2 = this.xmlHelper.GetAttribute("Y");
			if (!string.IsNullOrEmpty(attribute) && !string.IsNullOrEmpty(attribute2))
			{
                base.Location = new System.Drawing.Point(Convert.ToInt32(attribute), Convert.ToInt32(attribute2));
			}
		}

		private void ShowClearHeightForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private const string Root = "Root";

		private const string Node = "CheckTypeSelectForm";

		private const string ChildNode = "Params";

		private const string LocationX = "X";

		private const string LocationY = "Y";

		public Dictionary<Level, List<ClearHeightArea>> m_LevelItemInfo = new Dictionary<Level, List<ClearHeightArea>>();

		private RequestHandler m_Handler;

		private ExternalEvent m_ExEvent;

		private YJKCodeBase.XmlUtility.XmlHelper xmlHelper = new YJKCodeBase.XmlUtility.XmlHelper();

		private string path = Path.Combine(Product.WorkingLocation, "CheckTypeSelectForm.xml");
	}
}
